﻿using System;
using gms.data;
using gms.security;
using gms.system;

namespace gms.page.admin {
	public partial class aclmgr_add : GMSPage {
		public bool posted = false;

		public aclmgr_add() : base() {
			this.Load += new EventHandler(aclmgr_add_Load);
		}

		void aclmgr_add_Load(object sender, EventArgs e) {
			Authorization.AuthorizeOrForward(AclType.ManageAclgroups);

			if (Request.Form.Count != 0) {
				posted = true;
			}

			Acl_Group new_group = new Acl_Group();

			if (posted) {
				if (Request.Form["group_name"] != null) {
					if (Request.Form["group_name"].Length != 0) {
						new_group.name = Request.Form["group_name"];
					}
					else {
						posted = false;
						throw new Exception("name must not be empty");
					}
				}
				else {
					posted = false;
					throw new Exception("name must not be empty");
				}

				if (Request.Form["group_desc"] != null) {
					if (Request.Form["group_desc"].Length != 0) {
						new_group.description = Request.Form["group_desc"];
					}
				}

				if (Request.Form["manage_players"] != null) {
					new_group.acl_bits += (long)AclType.ManagePlayers;
				}

				if (Request.Form["manage_aclgroups"] != null) {
					new_group.acl_bits += (long)AclType.ManageAclgroups;
				}

				if (Request.Form["manage_plugins"] != null) {
					new_group.acl_bits += (long)AclType.ManagePlugins;
				}

				if (Request.Form["manage_opcodes"] != null) {
					new_group.acl_bits += (long)AclType.ManageOpcodes;
				}

				if (Request.Form["manage_flaggedcontent"] != null) {
					new_group.acl_bits += (long)AclType.ManageFlaggedContent;
				}

				if (Request.Form["manage_campaigns"] != null) {
					new_group.acl_bits += (long)AclType.ManageCampaigns;
				}

				if (Request.Form["manage_uploads"] != null) {
					new_group.acl_bits += (long)AclType.ManageUploads;
				}

				if (Request.Form["create_campaigns"] != null) {
					new_group.acl_bits += (long)AclType.CreateCampaigns;
				}

				if (Request.Form["create_uploads"] != null) {
					new_group.acl_bits += (long)AclType.CreateUploads;
				}
			}

			if (posted) {
				nhdb.session.BeginTransaction();
				nhdb.session.Save(new_group);
				nhdb.session.Transaction.Commit();

				Response.Redirect("aclmgr.aspx");
			}
		}
	}
}